//: C04: Strfile.cpp
// Stream I/O with files:
// The difference between get() & getline().
#include "../require.h"
#include <fstream>
#include <iostream>
using namespace std;

int main(void) {
  const int SZ = 100; // Buffer size;
  char buf[SZ];
  {
    ifstream in("Strfile.cpp");  // Read
    assure(in, "Strfile.cpp");   // Verify open
    ofstream out("Strfile.cpp"); // Write
    assure(out, "Strfile.out");
    int i = 1; // Line counter

    // A less-convenient approach for line input:
    while (in.get(buf, SZ)) {
      // Leaves \n in input
      in.get();            // Throw way next character(\n)
      cout << buf << endl; // Must add \n
      // File output jsut like standard I/O:
      out << i++ << ": " << buf << endl;
    }
  } // Destructors close in & out

  ifstream in("Strfile.out");
  assure(in, "Strfile.out");
  // More convenient line input:
  while (in.getline(buf, SZ)) {
    // Removes \n
    char *cp = buf;
    while (*cp != ':') {
      ++cp;   
    }
    cp +=2;   // Past ": "
    cout << cp << endl;    // Must still add \n
  }
}